package com.core.common;

import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

/**
 * 工具类
 */
public class NumKit {

    /**
     * 转
     *
     * @param values
     * @return
     */
    public static List<Long> toLong(String values) {
        if (values == null || values.length() == 0) {
            return new ArrayList<>();
        }
        String[] vArr = values.split(",");
        List<Long> resList = new ArrayList<>();
        for (String e : vArr) {
            try {
                resList.add(Long.valueOf(e));
            } catch (Exception ex) {
            }
        }
        return resList;
    }

    /**
     * 转
     *
     * @param dbList
     * @return
     */
    public static List<Long> toLong(Collection<String> dbList) {
        if (dbList == null || dbList.size() == 0) {
            return new ArrayList<>();
        }
        List<Long> resList = new ArrayList<>();
        for (String e : dbList) {
            try {
                resList.add(Long.valueOf(e));
            } catch (Exception ex) {

            }

        }
        return resList;
    }

    /**
     * 求和Integer
     *
     * @param dataCollection
     * @param function
     * @return
     */
    public static <T> Number sumInteger(Collection<T> dataCollection, Function<T, Integer> function) {
        Integer sum = 0;
        if (dataCollection == null || dataCollection.size() == 0) {
            return sum;
        }

        Integer e;
        for (T t : dataCollection) {
            e = function.apply(t);
            if (e != null) {
                sum = sum + e;
            }
        }
        return sum;
    }


    /**
     * 求和Double
     *
     * @param dataCollection
     * @param function
     * @return
     */
    public static <T> Double sumDouble(Collection<T> dataCollection, Function<T, Double> function) {
        Double sum = 0d;
        if (dataCollection == null || dataCollection.size() == 0) {
            return sum;
        }

        Double e;
        for (T t : dataCollection) {
            e = function.apply(t);
            if (e != null) {
                sum = sum + e;
            }
        }
        return sum;
    }

    /**
     * 求和BigDecimal
     *
     * @param dataCollection
     * @param function
     * @return
     */
    public static <T> BigDecimal sumBigDecimal(Collection<T> dataCollection, Function<T, BigDecimal> function) {
        BigDecimal sum = BigDecimal.ZERO;
        if (dataCollection == null || dataCollection.size() == 0) {
            return sum;
        }

        BigDecimal e;
        for (T t : dataCollection) {
            e = function.apply(t);
            if (e != null) {
                sum = NumberUtil.add(sum, e);
            }
        }
        return sum;
    }


}
